package com.cmes.mes.basic.web.controller;

import com.cmes.core.Pagination;
import com.cmes.core.domain.bo.IdsBO;
import com.cmes.core.utils.BeanUtils;
import com.cmes.core.utils.CollectionUtils;
import com.cmes.jdbc.customquery.CustomQueryBO;
import com.cmes.mes.basic.api.bo.AuditBatchBO;
import com.cmes.mes.basic.api.bo.CustomerBO;
import com.cmes.mes.basic.core.biz.CustomerBizService;
import com.cmes.mes.basic.core.entity.CustomerEntity;
import com.cmes.mes.basic.core.service.CustomerService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.Comparator;
import java.util.List;

/**
 * 客户
 *
 * @author Bruce.Gong
 * @since 1.0.0-SNAPSHOT
 */
@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("/mes/customer")
public class CustomerController {

    private final CustomerBizService customerBizService;
    private final CustomerService customerService;

    @PostMapping("search")
    public Pagination<CustomerEntity> search(CustomQueryBO bo) {
        return customerService.page(bo);
    }

    // region 增删查改

    @PostMapping("add")
    public Long add(@RequestBody @Valid CustomerBO.AddBO bo) {
        CustomerEntity entity = BeanUtils.copyProperties(bo, new CustomerEntity());
        customerBizService.canAdd(entity);
        return customerBizService.add(entity);
    }

    @PostMapping("update")
    public Long update(@RequestBody @Valid CustomerBO.UpdateBO bo) {
        CustomerEntity entity = BeanUtils.copyProperties(bo, new CustomerEntity());
        CustomerEntity db = customerBizService.canUpdate(entity);
        return customerBizService.update(entity, db);
    }

    @PostMapping("delete")
    public void delete(@RequestBody @Valid IdsBO bo) {
        List<CustomerEntity> dbs = customerBizService.canDelete(bo.getIds());
        customerBizService.delete(bo.getIds(), dbs);
    }

    // endregion

    // region 审核流程

    @PostMapping("submit")
    public void submit(@RequestBody @Valid AuditBatchBO bo) {
        customerBizService.submit(bo.getIds(), customerBizService.canSubmit(bo.getIds()), bo.getCause());
    }

    @PostMapping("reject")
    public void reject(@RequestBody @Valid AuditBatchBO bo) {
        customerBizService.reject(bo.getIds(), customerBizService.canReject(bo.getIds()), bo.getCause());
    }

    @PostMapping("audit")
    public void audit(@RequestBody @Valid AuditBatchBO bo) {
        customerBizService.audit(bo.getIds(), customerBizService.canAudit(bo.getIds()), bo.getCause());
    }

    @PostMapping("disable")
    public void disable(@RequestBody @Valid AuditBatchBO bo) {
        customerBizService.disable(bo.getIds(), customerBizService.canDisable(bo.getIds()), bo.getCause());
    }

    // endregion

    @PostMapping("selector")
    public List<CustomerEntity> selector() {
        return CollectionUtils.listTree(customerService.activeCustomer(),
                0L,
                CustomerEntity::getId,
                CustomerEntity::getPid,
                CustomerEntity::setChildren,
                null,
                Comparator.comparing(CustomerEntity::getId));
    }

}
